#获取艾瑞网最新报告
import requests
from bs4 import BeautifulSoup
import json
# 发起HTTP请求获取网页内容
url = 'https://www.stats.gov.cn/sj/zxfb/'

response = requests.get(url)
response.encoding = 'UTF-8' # 或者其他正确的编码方式

# 解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')

# print(soup)

# # # 提取新闻内容
# news_content = soup.find('div', class_='list-content').get_text()
#
# # 打印新闻内容
# print(news_content)

# 查找所有class为list-content的div元素下的li标签
items = soup.find('div', class_='list-content').find_all('li')

c=0     #计数器
# 初始化一个空字符串
result_str = "国家统计局数据\n"
# 遍历每个li标签，提取title、href和日期信息
for item in items:
    c = c + 1
    a_tag = item.find('a')  # 获取第一个<a>标签，假设都是一样的
    title = a_tag.get('title')
    href = a_tag.get('href')
    date_span = item.find('span').get_text(strip=True)  # 获取<span>标签中的日期信息
    # print(f'Title: {title}, Href: {href}, Date: {date_span}')
    result_str += f'{c}: <a href=\"{url}{href}\">{title}</a>   {date_span}\n'
    result_str += "=======================\n"
print(result_str)

#发送vx消息
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d230f478-24eb-4780-a45d-0e8fe951bded'
headers = {'Content-Type': 'application/json'}
#文本
#带超链接
#article ="<a href=\"http://baidu.com\">邮件中心视频实况</a>"


data = {
    "msgtype": "text",
    "text": {
        "content": result_str
    },
    "safe": 0,
    "enable_id_trans": 0,
    "enable_duplicate_check": 0,
    "duplicate_check_interval": 1800
}
response = requests.post(url, headers=headers, data=json.dumps(data))
